using System;
using System.IO;
using QFramework;
using QFramework.Game;
using UnityEngine;

namespace QGameFramework.License
{
    public class LicenseComponent : BaseComponent
    {
        /// <summary>
        /// 许可证SO文件
        /// </summary>
        [Header("许可证SO文件")] public LicenseSO m_LicenseSO;

        /// <summary>
        /// 是否过期
        /// </summary>
        public bool m_IsExpired { get; private set; }

        public override void OnInit()
        {
            base.OnInit();
            m_IsExpired = DetectExpired();
        }

        public override void DeInit()
        {
            base.DeInit();
        }

        /// <summary>
        /// 创建许可证
        /// </summary>
        [ContextMenu("创建许可证")]
        public void CreateLicense()
        {
            if (!File.Exists(Application.streamingAssetsPath + "/License/License.ML"))
            {
                var license = m_LicenseSO.m_License;
                QGFC.Instance.Tool.m_Tool.WriteBin2File<License>("/License/License.ML", license);
                LogKit.E($"创建成功！ 时间{license.m_YMD.m_Year}:{license.m_YMD.m_Month}:{license.m_YMD.m_Day}:{license.m_HMS.m_Hour}" +
                         $":{license.m_HMS.m_Minute}:{license.m_HMS.m_Second}");
            }
        }


        /// <summary>
        /// 许可证是否过期检测
        /// </summary>
        /// <returns>是否过期</returns>
        private bool DetectExpired()
        {
#if UNITY_EDITOR
            var lockTime = new DateTime(m_LicenseSO.m_License.m_YMD.m_Year, m_LicenseSO.m_License.m_YMD.m_Month, m_LicenseSO.m_License.m_YMD.m_Day,
                m_LicenseSO.m_License.m_HMS.m_Hour, m_LicenseSO.m_License.m_HMS.m_Minute, m_LicenseSO.m_License.m_HMS.m_Second);
            if (DateTime.Now > lockTime)
            {
                LogKit.E($"用户：{m_LicenseSO.m_License.m_UID}授权文件过期");
                return true;
            }
            else
            {
                LogKit.E($"用户：{m_LicenseSO.m_License.m_UID}授权文件未过期");
                return false;
            }

# elif UNITY_STANDALONE_WIN
            if (QGFC.Instance.Tool.m_Tool.ReadBin2File<License>("/License/License.ML", out License license))
            {
                LogKit.E(license.m_IsLock);
                if (!license.m_IsLock)
                {
                    var lockTime = new DateTime(license.m_YMD.m_Year, license.m_YMD.m_Month, license.m_YMD.m_Day, license.m_HMS.m_Hour, license.m_HMS.m_Minute,
                        license.m_HMS.m_Second);

                    if (DateTime.Now > lockTime)
                    {
                        license.m_IsLock = true;
                        QGFC.Instance.Tool.m_Tool.WriteBin2File<License>("/License/License.ML", license);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return true;
                }
            }
            else
            {
                CreateLicense();
                return true;
            }
#endif
        }
    }
}